package com.xinxing.learning.batch.mybatisplus.config;

import cn.hutool.core.collection.CollUtil;
import com.xinxing.learning.batch.entity.User;
import com.xinxing.learning.batch.mybatisplus.listener.MybatisPlusSqlJobEndListener;
import com.xinxing.learning.batch.mybatisplus.step.UserItemProcessor;
import com.xinxing.learning.batch.mybatisplus.step.UserItemReader;
import com.xinxing.learning.batch.mybatisplus.step.UserItemWriter;
import org.mybatis.spring.batch.builder.MyBatisCursorItemReaderBuilder;
import org.mybatis.spring.batch.builder.MyBatisPagingItemReaderBuilder;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
@EnableBatchProcessing
public class MybatisPlusSqlBatchConfig {
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job mybatisPlusSqlJob(Step mybatisPlusSqlStep, JobExecutionListener mybatisPlusSqlListener) {
        String funcName = Thread.currentThread().getStackTrace()[1].getMethodName();
        return jobBuilderFactory.get(funcName)
                .listener(mybatisPlusSqlListener)
                .flow(mybatisPlusSqlStep)
                .end().build();
    }

    @Bean
    public Step mybatisPlusSqlStep(ItemReader mybatisPlusSqlItemReader, ItemProcessor mybatisPlusSqlProcessor, ItemWriter mybatisPlusSqlWriter) {
        String funcName = Thread.currentThread().getStackTrace()[1].getMethodName();
        new MyBatisPagingItemReaderBuilder<User>();
        new MyBatisCursorItemReaderBuilder<User>();
        return stepBuilderFactory.get(funcName)
                .<User, User>chunk(10)
                .reader(mybatisPlusSqlItemReader)
                .processor(mybatisPlusSqlProcessor)
                .writer(mybatisPlusSqlWriter)
                .build();
    }

    @Bean
    public ItemReader mybatisPlusSqlItemReader() {
        UserItemReader userItemReader = new UserItemReader();
        //设置参数，当前示例可不设置参数
        Map<String, Object> params = CollUtil.newHashMap();
        // userItemReader.setParams(params);

        return userItemReader;
    }

    @Bean
    public ItemWriter mybatisPlusSqlWriter() {
        return new UserItemWriter();
    }

    @Bean
    public ItemProcessor mybatisPlusSqlProcessor() {
        return new UserItemProcessor();
    }

    @Bean
    public JobExecutionListener mybatisPlusSqlListener() {
        return new MybatisPlusSqlJobEndListener();
    }
}
